<?php
namespace app\ucentre\controller;

use app\ucentre\WenBaseController;
use app\ucentre\model\User;
use app\ucentre\model\WendidUser;
use app\ucentre\model\Log;
use app\ucentre\service\PwUser;
use wendasns\facade\Email;
use wendasns\facade\Wendid;
use wendasns\facade\Image;
use wendasns\Wend;
use think\helper\Str;
use think\Exception;

class Index extends WenBaseController
{
    public function index()
    {
    	return view();
    }

    public function info()
    {
    	$mobile = input('post.phone', '');
    	$email = input('post.email', '');

        //验证数据
        try {
            validate(\app\ucentre\validate\User::class)->check([
                'phone' => $mobile,
                'email' => $email
            ]);
        } catch (\think\exception\ValidateException $e) {
            error($e->getError());
        }
        
        try {
            $user = new PwUser();
            $user->id = $this->loginUser->uid;
            $user->mobile = $mobile;
            $user->email = $email;
            $user->update();
        } catch (\think\Exception $e) {
            error($e->getMessage());
        }

    	success('操作完成');
    }

    public function password()
    {
    	$password = input('post.password', '');
    	$repassword = input('post.repassword', '');
        //验证数据，包括敏感词
        try {
            validate(\app\ucentre\validate\User::class)->check([
                'password' => $password,
                'repassword' => $repassword
            ]);
        } catch (\think\exception\ValidateException $e) {
            error($e->getError());
        }

        try {
            $user = new PwUser();
            $user->id = $this->loginUser->uid;
            $user->password = $password;
            $user->update();
        } catch (\think\Exception $e) {
            error($e->getMessage());
        }

        success('操作完成');
    }
    
    public function loginlog()
    {
    	$limit = input('get.limit', 10);
    	$dm = Log::where('user_id',$this->loginUser->uid)->order('create_time', 'desc')->paginate($limit);
    	$data = $dm->toArray();
    	success('ok',$data['data'],['count'=>$data['total']]);
    }
    
    //上传头像
    public function upload()
    {
        try {
        	$file = request()->file('file');
        	Image::portrait($this->loginUser->uid)->upload($file);
        } catch (Exception $e) {
            error($e->getMessage());
        }
    	success('上传成功',['src'=>'', 'title'=>'']);
    }
}